package com.nodecollege.cloud.controller;

import com.nodecollege.cloud.common.annotation.ApiAnnotation;
import com.nodecollege.cloud.common.model.*;
import com.nodecollege.cloud.common.model.po.UpmsMember;
import com.nodecollege.cloud.common.model.po.UpmsOrg;
import com.nodecollege.cloud.common.model.po.UpmsRole;
import com.nodecollege.cloud.common.model.po.UpmsUserInvitation;
import com.nodecollege.cloud.common.utils.NCUtils;
import com.nodecollege.cloud.common.utils.ThreadUserUtils;
import com.nodecollege.cloud.common.annotation.UserLoginAnnotation;
import com.nodecollege.cloud.service.MemberService;
import com.nodecollege.cloud.service.OrgMemberService;
import com.nodecollege.cloud.service.RoleMemberService;
import com.nodecollege.cloud.service.UserInvitationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;


/**
 * 成员管理
 * @author LC
 * @date 2019/12/12 15:26
 */
@RestController
@UserLoginAnnotation(checkDataPower = true)
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private UserInvitationService userInvitationService;

    @Autowired
    private OrgMemberService orgMemberService;

    @Autowired
    private RoleMemberService roleMemberService;

    /**
     * 查询成员列表
     *
     * @param queryVO
     */
    @ApiAnnotation(modularName = "成员管理", description = "查询成员列表")
    @PostMapping("/getMemberList")
    public NCResult getMemberList(@RequestBody QueryVO<UpmsMember> queryVO) {
        ApiDataPower apiDataPower = ThreadUserUtils.getLoginUser().getApiDataPower();
        apiDataPower.check(queryVO);
        apiDataPower.handle(queryVO);
        return memberService.getMemberList(queryVO);
    }

    /**
     * 邀请成员
     *
     * @param invitation
     */
    @ApiAnnotation(modularName = "成员管理", description = "邀请成员")
    @PostMapping("/inviteMember")
    public NCResult inviteMember(@RequestBody UpmsUserInvitation invitation) {
        LoginUser loginUser = ThreadUserUtils.getLoginUser();
        invitation.setInviteMemberId(loginUser.getMemberId());
        invitation.setInviteTenantId(loginUser.getTenantId());
        userInvitationService.inviteMember(invitation);
        return NCResult.ok("邀请成员成功！");
    }

    /**
     * 更新成员信息
     * @param upmsMember
     * @return
     */
    @ApiAnnotation(modularName = "成员管理", description = "更新成员信息")
    @PostMapping("/updateMember")
    public NCResult updateMember(@RequestBody UpmsMember upmsMember){
        upmsMember.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        memberService.updateMember(upmsMember);
        return NCResult.ok("修改用户成功！");
    }

    /**
     * 删除成员
     * @param upmsMember
     * @return
     */
    @ApiAnnotation(modularName = "成员管理", description = "删除成员")
    @PostMapping("/delMember")
    public NCResult delMember(@RequestBody UpmsMember upmsMember){
        upmsMember.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        memberService.delMember(upmsMember);
        return NCResult.ok("删除用户成功！");
    }

    @ApiAnnotation(modularName = "成员管理", description = "锁定/解锁成员")
    @PostMapping("/lockMember")
    public NCResult lockMember(@RequestBody UpmsMember upmsMember){
        upmsMember.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        memberService.lockMember(upmsMember);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "成员管理", description = "查询成员拥有组织列表")
    @PostMapping("/getOrgListByMemberId")
    public NCResult getOrgListByMemberId(@RequestBody QueryRelationVO<UpmsOrg, UpmsMember> queryVO) {
        ApiDataPower apiDataPower = ThreadUserUtils.getLoginUser().getApiDataPower();
        apiDataPower.check(queryVO);
        queryVO.setOrgId(null);
        queryVO.setMemberId(null);
        return orgMemberService.getOrgListByMemberId(queryVO);
    }

    @ApiAnnotation(modularName = "成员管理", description = "绑定/解绑组织机构成员关系")
    @PostMapping("/bindOrgMemberByMemberId")
    public NCResult bindOrgMemberByMemberId(@RequestBody BindVO bindVO) {
        bindVO.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        bindVO.setMainSource(false);
        orgMemberService.bindOrgMember(bindVO);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "成员管理", description = "查询成员拥有角色列表")
    @PostMapping("/getRoleListByMemberId")
    public NCResult getRoleListByMemberId(@RequestBody QueryRelationVO<UpmsRole, UpmsMember> queryVO) {
        queryVO.getEndNode().setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        return roleMemberService.getRoleListByMemberId(queryVO);
    }

    @ApiAnnotation(modularName = "成员管理", description = "绑定/解绑角色成员关系")
    @PostMapping("/bindRoleMemberByMemberId")
    public NCResult bindRoleMemberByMemberId(@RequestBody BindVO bindVO) {
        bindVO.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        bindVO.setMainSource(false);
        roleMemberService.bindRoleMember(bindVO);
        return NCResult.ok();
    }
}
